#include <algorithm>
#include <cstdio>
using namespace std;

int t, m, minn = 0x3f3f3f3f;
int w[101], f[1000], v[101];

int main() {
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
	scanf("%d %d", &t, &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d %d", w + i, v + i);
		minn = min(minn, w[i]);
	}
	for (int i = 1; i <= m; ++i)
		for (int j = t; j >= minn; --j) {
			if (j >= w[i])
				f[j] = max(f[j], f[j - w[i]] + v[i]);
		}

	printf("%d", f[t]);
	return 0;
}